package main

import (
	"encoding/json"
	"fmt"
	"github.com/gogf/gf/g"
	"github.com/gogf/gf/g/os/glog"
	"github.com/gogf/gf/g/util/gconv"
)

type hget struct {
	FacPort      string
	OnlineTime   string
	UpdateTime   string
	OnlineStatus string
	IpAddress    string
	Port         string
	DeviceNo     string
	ConnID       string
}

func main() {
	// 使用默认文件名称格式
	var maps = make([]map[string]string, 0)
	maps=dowork("0",maps)
	str, err := json.Marshal(maps)
	if err != nil {
		glog.Error(err)
		return
	}
	fmt.Println("map to json", string(str))
	var last []hget
	e := json.Unmarshal(str, &last)
	if err != nil {
		glog.Error(e)
		return
	}
	for _, value := range last {
		glog.Println("last ", value.ConnID, "======", value.OnlineTime," OnlineStatus====",value.OnlineStatus)
	}
}

func dowork(index string,maps []map[string]string) []map[string]string{
	if maps==nil{
		maps=make([]map[string]string, 0)
	}
	if keys, err := g.Redis().Do("scan", index, "match", "Up*"); err == nil { //,"match Up* ","count 100"
		if v, ok := keys.([]interface{}); ok {
			s := gconv.String(v[0])
			m := gconv.Strings(v[1])
			for _, value := range m {
				fmt.Println("value====", gconv.String(value))
				if v, err := g.Redis().Do("hgetall", gconv.String(value)); err == nil {
					strs := gconv.Strings(v)
					fmt.Println("v==========", strs)
					smap := make(map[string]string)
					for i := 0; i < len(strs); i = i + 2 {
						smap[strs[i]] = strs[i+1]
					}
					maps = append(maps, smap)
				} else {
					glog.Error(err)
				}
			}
			if s != "0" {
				return dowork(gconv.String(s),maps)
			} else {
				fmt.Println("work done")
			}
		}
	} else {
		glog.Error(err)
	}
	return maps
}
